package code.java.lang;

import java.io.UnsupportedEncodingException;

/**
 * Created by chopin on 17/12/16.
 */
public class StringTest {

    public static void main(String[] args){

        StringTest t = new StringTest();
        t.tesetGetBytes();

    }
    public void tesetGetBytes(){
        String s1 = "中文01bc";//传说中一个中文字符占3个字节
        this.getBytes(s1,"UTF-8");//长度10，中文字符占3个字节
        this.getBytes(s1,"GBK");//长度8，中文字符占2个字节
    }

    public void getBytes(String str,String charset){
        try {
            int length = str.getBytes(charset).length;
            System.out.println(str+", "+charset+", length = "+length);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}
